#!/usr/bin/env bash
# Tag and push a release.

set -e

# Make sure we're in the project root.

cd $(dirname "$0")/..

# Make sure the darn thing works

npm update && script/smoke-test

# Make sure we're on the main branch.

(git branch | grep -q '* main') || {
  echo "Only release from the main branch."
  exit 1
}

# Figure out what version we're releasing.

tag=v`node -e 'console.log(require("./package.json").version)'`

# Ensure there's a line in the CHANGELOG

grep "$tag" CHANGELOG.md || {
  echo "No entry for '$tag' found in the CHANGELOG."
  exit 1
}

# Make sure we haven't released this version before.

git fetch -t origin

(git tag -l | grep -q "$tag") && {
  echo "Whoops, there's already a '${tag}' tag."
  exit 1
}

# Tag it and bag it.

npm publish && git tag "$tag" &&
  git push origin main --tags
